package com.comp437.uno.view.login;

import java.util.Map;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.comp437.uno.model.ClientService;
import com.comp437.uno.model.ClientService.LocalBinder;
import com.comp437.uno.model.Model;
import com.comp437.uno.view.playroomlist.PlayRoomListView;
import com.example.comp437uno.R;

public class Login extends Activity{
	private Model model;
	private Button bt_SignIn = null;
	private Button bt_SignUp = null;
	private ImageButton bt_Settings = null;
	private EditText username = null;
	private EditText password = null;
	private String user = null;
	private String pw = null;
	Intent service;
	
	ClientService cs;
	boolean myBound=false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		model=Model.getModel();
		model.setLogin(this);
		model.setContext(Login.this);
		username = (EditText) findViewById(R.id.edittext_login_username);
		password = (EditText) findViewById(R.id.edittext_login_password);
		bt_Settings = (ImageButton) findViewById(R.id.imagebutton_login_settings);
		bt_SignIn = (Button) findViewById(R.id.button_login_signin);
		bt_SignUp = (Button) findViewById(R.id.button_login_signup);
		bt_SignIn.setOnClickListener(new bt_SignInListener());
		bt_SignUp.setOnClickListener(new bt_SignUpListener());
		bt_Settings.setOnClickListener(new bt_SettingsListener());
		
		Map<String, String> map = model.readFile(Login.this);  
		  if (map != null) {  
			  username.setText(map.get("name"));  
			  password.setText(map.get("pass"));  
		  } 
		
		service = new Intent(Login.this, ClientService.class);
		startService(service);
		 bindService(service, mConnection, Context.BIND_AUTO_CREATE);
		
		try{
	    
	        model.showToast(Login.this, "Checking");
	        boolean isInternetPresent = model.isConnectingToInternet(getApplicationContext());

	        //boolean isInternetPresent =true; 
		        if(!isInternetPresent){        	  
		        	  model.showToast(Login.this, "No Network Connection!");
		        }
		        else
		        	model.showToast(Login.this, "Internet Works!");
	        }catch(Exception e){
       
	        }
		
		
	}
	class bt_SignInListener implements android.view.View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			 startService(service);
			
			user = username.getText().toString();
			
			pw = password.getText().toString();
			
			
			if(user!=null && pw!=null){
				if(user=="")
					model.showToast(Login.this, "Please Input the Username");
				else if(pw=="")
					model.showToast(Login.this, "Please Input the Password");
				else {
					cs.login(user, pw,0);
				}
			}
		}
		
	}
	
	public void signInSuccessfully(String str){

		if(str!=null){
			
			if(!str.equals("")){
				model.saveUserNameAPasswordToRom(user,pw, Login.this);
				Intent intent=new Intent();
				intent.putExtra("username", str.substring(8));
				
				intent.setClass(Login.this, PlayRoomListView.class);
				Login.this.startActivity(intent);
			}else{
				model.showToast(Login.this, "Wrong Username or Password");
			}
		}else
			model.showToast(Login.this,"reply null");
	}
	
	class bt_SignUpListener implements android.view.View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
		
			Intent intent=new Intent();
			intent.setClass(Login.this, SignUp.class);
			Login.this.startActivity(intent);

		}
	}
	
	class bt_SettingsListener implements android.view.View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub

			Intent intent=new Intent();
			intent.setClass(Login.this, Settings.class);
			Login.this.startActivity(intent);

		}
	}
	
	private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            cs = binder.getService();
            model.setClientService(cs);
            myBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            myBound = false;
        }
    };

	
}
